package com.buddy.learn.network.socekt;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

/**
 * @author WongZP
 * @date 2022/4/17
 */
public class SocketClient {

    public static void main(String[] args) {
            try {
                // 1. 创建 socket 对象
                Socket s = new Socket("127.0.0.1",8088);
                // 2. 从连接中获取输出流并发消息
                OutputStream os = s.getOutputStream();
                os.write("hello socket!".getBytes(StandardCharsets.UTF_8));
                System.out.println(" client send.....");
                InputStream is = s.getInputStream();
                byte[] b = new byte[1024];
                int read = is.read(b);
                System.out.println("client receive >>> "+new String(b,0,read).trim());

            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
    }
}
